package sword.T15;

/**
 * @File Info: leetcode -- <Solution>
 * @Author: 18362
 * @Create: 2022-03-23 17:00:48 星期三
 */

public class Solution {
    // you need to treat n as an unsigned value
    // 一刷
    /*public int hammingWeight(int n) {
        int res = 0;
        if (n < 0) {
            res++;
            n = n + 1 + Integer.MAX_VALUE;
        }
        while (n > 0) {
            if (n % 2 == 1) {
                res++;
            }
            n /= 2;
        }
        return res;
    }*/

    // 二刷（位运算解法）
    public int hammingWeight(int n) {
        int res = 0;
        while (n != 0) {
            res += n & 1;
            n = n >>> 1;
        }
        return res;
    }
}